﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Web.Support;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Cellula.Model;
using System.Collections.Generic;
using Cn.Edwon.Web;
using Cn.Cellula.Model.System;
using Cn.Cellula.Client;

namespace Cn.Cellula.Website.Admin.Content.Code {
    public partial class CodeEdit : PageSupport {
        private String CodeKey = null;
        private String TypeKey = null;
        ICodeBiz biz = BizFactory.Get<ICodeBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            CodeKey = Request.QueryString["id"];
            TypeKey = Request.QueryString["t"];
            if (!Page.IsPostBack) {
                InitForm();
                if (!String.IsNullOrEmpty(CodeKey)) {
                    FillForm(CodeKey);
                }
            }
        }
        private void FillForm(String codeKey) {
            TSysCodeValueEO eo = biz.Load<TSysCodeValueEO>(codeKey);
            this.txt_CodeName.Text = eo.CodeName;
            this.txt_CodeKey.Text = eo.CodeKey;
            this.ddl_CodeType.SelectedValue = eo.TypeKey;
            this.hid_OldCodeName.Value = eo.CodeName;
            this.txt_OrderNumber.Text = eo.OrderNumber + "";
            this.hid_OldCodeValue.Value = eo.CodeKey;
        
        
        }
        private void InitForm() {
            IList<TSysCodeTypeEO> list = biz.LoadAll<TSysCodeTypeEO>();
            this.ddl_CodeType.DataTextField = "TypeName";
            this.ddl_CodeType.DataValueField = "TypeKey";
            this.ddl_CodeType.DataSource = list;
            this.ddl_CodeType.DataBind();
            if (!String.IsNullOrEmpty(TypeKey)) {
                this.ddl_CodeType.SelectedValue = TypeKey;
            }
            this.txt_OrderNumber.Text = "0";
        
            
        }

        private void BindData2EO(ref TSysCodeValueEO eo) {
            eo.CodeName = this.txt_CodeName.Text;
            eo.TypeKey = this.ddl_CodeType.SelectedValue;
            eo.CodeKey = this.txt_CodeKey.Text;
            try {
                eo.OrderNumber = Convert.ToInt32(this.txt_OrderNumber.Text);
            } catch {
                eo.OrderNumber = 0;
            }
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            if (String.IsNullOrEmpty(CodeKey)) {
                TSysCodeValueEO eo = new TSysCodeValueEO();
                BindData2EO(ref eo);
                //检查Key是否存在
                if (biz.IfCodeNameExist(eo.CodeName)) {
                    SiteInfo.Send(Page, TxtDef.ERROR, "名称已存在，请重新输入");
                    return;
                }
                try {
                    if (biz.Load<TSysCodeValueEO>(eo.CodeKey) != null) {
                        SiteInfo.Send(Page, TxtDef.ERROR, "值已存在，请重新输入");
                        return;
                    }
                } catch { }
                biz.Save<TSysCodeValueEO>(eo);
                BizFactory.Get<ILogBiz>().LogInsert("代码值", eo.CodeName, this.Actor, Request);
            } else {
                TSysCodeValueEO eo = biz.Load<TSysCodeValueEO>(CodeKey);
                BindData2EO(ref eo);
                if (this.hid_OldCodeName.Value != eo.CodeName && biz.IfCodeNameExist(eo.CodeName)) {
                    SiteInfo.Send(Page, TxtDef.ERROR, "名称已存在，请重新输入");
                    return;
                }
                try {
                    if (CodeKey!=eo.CodeKey && biz.Load<TSysCodeValueEO>(eo.CodeKey) != null) {
                        SiteInfo.Send(Page, TxtDef.ERROR, "值已存在，请重新输入");
                        return;
                    }
                } catch { }
                biz.UpdateCodeValue(eo, this.hid_OldCodeValue.Value);
                BizFactory.Get<ILogBiz>().LogUpdate("代码值", eo.CodeName, this.Actor, Request);
            }
            Response.Redirect("TypeList.aspx?t=" + this.ddl_CodeType.SelectedValue);
            //SiteInfo.SendSucceed(Page, "TypeList.aspx?t=" + eo.TypeKey);
        }
    }
}
